var dataApiService = require("../../provider/dataApi.js");

getApp();

Page({
    data: {
        statusBarHeight: "",
        titleBarHeight: "",
        pageData: {}
    },
    onLoad: function(options) {
        this.setData({
            statusBarHeight: getApp().globalData.statusBarHeight,
            titleBarHeight: getApp().globalData.titleBarHeight
        });
        this.initializeWechat();
    },
    goBack: function() {
        wx.navigateBack({
            delta: 1
        });
    },
    initializeWechat: function() {
        var self = this;
        dataApiService.getBind({
            uid: wx.getStorageSync("uid")
        }).then(function(apiResponse) {
            console.log(apiResponse);
            if (apiResponse.errno === 0) {
                self.setData({
                    pageData: apiResponse.data.data
                });
            }
        }).catch(function(apiError) {
            console.log(apiError);
        });
    },
    bindWechat: function() {
        var self = this;
        wx.showLoading({
            title: "绑定中..."
        });
        wx.login({
            success: function(loginResponse) {
                console.log(loginResponse);
                var loginCode = loginResponse.code;
                wx.getSetting({
                    success: function(settingResponse) {
                        console.log(settingResponse);
                        if (settingResponse.authSetting["scope.userInfo"]) {
                            wx.getUserInfo({
                                success: function(userInfoResponse) {
                                    console.log(userInfoResponse);
                                    dataApiService.login({
                                        uid: wx.getStorageSync("uid"),
                                        code: loginCode,
                                        isBind: 2,
                                        encryptedData: userInfoResponse.encryptedData,
                                        iv: userInfoResponse.iv
                                    }).then(function(apiResponse) {
                                        console.log(apiResponse);
                                        wx.hideLoading();
                                        wx.showToast({
                                            title: apiResponse.message,
                                            icon: "none"
                                        });
                                        if (apiResponse.errno === 0) {
                                            self.initializeWechat();
                                        }
                                    }).catch(function(apiError) {
                                        console.log(apiError);
                                        wx.hideLoading();
                                        wx.showToast({
                                            title: apiError.message,
                                            icon: "none"
                                        });
                                    });
                                }
                            });
                        }
                    }
                });
            }
        });
    },
    unbindWechat: function() {
        wx.navigateTo({
            url: "../untieWechat/untieWechat"
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});